package com.example.lockspringbootstarter.config;

import com.example.lockspringbootstarter.lock.manager.LockFactory;
import com.example.lockspringbootstarter.lock.manager.LockManager;
import io.etcd.jetcd.Client;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
@EnableConfigurationProperties({LockProperties.class})
public class LockAutoConfiguration {

    @Autowired
    private LockProperties lockProperties;

    @Bean
    public LockFactory lockFactory(){
        return new LockFactory();
    }

    @Bean
    public LockManager lockManager(){
        return new LockManager(lockProperties.getLockType());
    }

    // @Bean
    // public ZooKeeper zooKeeper() {
    //     try {
    //         return new ZooKeeper(lockProperties.getZookeeperUrl(), 10000, null);
    //     } catch (IOException e) {
    //         throw new RuntimeException(e);
    //     }
    // }

    @Bean
    public Client etcdClient() {
        return Client.builder().endpoints(lockProperties.getEtcdUrl()).build();
    }
}